package com.example.mvc.service;

import com.example.mvc.bean.Book;
import com.example.mvc.bean.History;
import com.example.mvc.bean.User;
import com.example.mvc.mapper.BookBorrowMapper;
import com.example.mvc.mapper.BookMapper;
import com.example.mvc.mapper.UserMapper;
import com.example.mvc.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class BookBorrowService {
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();

    //查询所有
    public List<History> getBookList(){
        //获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //获取BrandMapper
        BookBorrowMapper mapper = sqlSession.getMapper(BookBorrowMapper.class);
        //调用方法
        List<History> borrowList =  mapper.getBorrowList();
        sqlSession.close();
        return borrowList;
    }

    //还书操作
    public int bookReturn(int returnBookId) {
        //2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //3. 获取BrandMapper
        BookBorrowMapper mapper = sqlSession.getMapper(BookBorrowMapper.class);
        //4. 调用方法
        //归还日期
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String returnDate = format.format(new Date());
        int count = mapper.bookReturn(returnBookId, returnDate);
        if (count>0){//还书库存要加库存数量
            BookMapper bookMapper = sqlSession.getMapper(BookMapper.class);
            Book bookById = bookMapper.getBookById(returnBookId);
            bookMapper.addBookNum(bookById);
        }
        //提交事务
        sqlSession.commit();
        //释放资源
        sqlSession.close();

        return count;


    }

    public List<History> bookAlreadyReturnedList() {
        //获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //获取BrandMapper
        BookBorrowMapper mapper = sqlSession.getMapper(BookBorrowMapper.class);
        //调用方法
        List<History> alreadyReturnedList =  mapper.bookAlreadyReturnedList();
        sqlSession.close();
        return alreadyReturnedList;
    }

    // 借出和归还的列表
    public List<History> bookHistoryList(){
        //获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //获取BrandMapper
        BookBorrowMapper mapper = sqlSession.getMapper(BookBorrowMapper.class);
        //调用方法
        List<History> borrowList =  mapper.bookHistoryList();
        sqlSession.close();
        return borrowList;
    }

    public List<History> getQueryBookBorrowHistoryListByName(String queryBookName) {
        //获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //获取BrandMapper
        BookBorrowMapper mapper = sqlSession.getMapper(BookBorrowMapper.class);
        //调用方法
        List<History> borrowList =  mapper.getQueryBookBorrowHistoryListByName(queryBookName);
        sqlSession.close();
        return borrowList;
    }

    public List<History> borrowRecordListServlet(int bookId) {
        //获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //获取BrandMapper
        BookBorrowMapper mapper = sqlSession.getMapper(BookBorrowMapper.class);
        //调用方法
        List<History> borrowList =  mapper.borrowRecordListServlet(bookId);
        sqlSession.close();
        return borrowList;
    }

    public int userBorrowBook(String  userId,String bookId){
        //获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //获取BrandMapper

        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        BookMapper bookMapper = sqlSession.getMapper(BookMapper.class);
        //通过用户id查询用户的信息
        User userById = userMapper.getUserById(Integer.parseInt(userId));
        //通过书的id查询图书的信息
        Book bookById = bookMapper.getBookById(Integer.parseInt(bookId));
        BookBorrowMapper borrowMapper = sqlSession.getMapper(BookBorrowMapper.class);
        //借书需要修改history表的状态，这里封装history方便些
        History history = new History();
        history.setAid(userById.getAid());
        history.setBid(bookById.getBid());
        history.setCard(bookById.getCard());
        history.setBookname(bookById.getName());
        history.setAdminname(userById.getUsername());
        history.setUsername(userById.getName());
        //生成日期功能
        Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH)+1;
        int day = c.get(Calendar.DATE);

        //生成借阅开始时间
        String begintime = ""+ year +"-"+month +"-"+day;
        month = month +1;
        //生成截至还书日期
        if (month>12){
            year+=1;
            month=1;
        }
        String endtime = ""+year+"-"+month+"-"+day;
        history.setBegintime(begintime);
        history.setEndtime(endtime);
        history.setStatus(1);
        //调用方法
        int count= borrowMapper.userBorrowBook(history);
        if (count>0){//借出了就要在数据库的库存减1
            bookMapper.reduceBookNum(bookById);
        }
        sqlSession.commit();
        sqlSession.close();
       return count;

    }

    public List<History> getBorrowBookListByUser(User user){
        //获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //获取BrandMapper
        BookBorrowMapper mapper = sqlSession.getMapper(BookBorrowMapper.class);
        //调用方法
        List<History> borrowList =  mapper.getBorrowBookListByUser(user);
        sqlSession.close();
        return borrowList;

    }

    public List<History> borrowBookHistoryListByUser(User user) {
        //获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //获取BrandMapper
        BookBorrowMapper mapper = sqlSession.getMapper(BookBorrowMapper.class);
        //调用方法
        List<History> borrowBookHistoryListByUser =  mapper.borrowBookHistoryListByUser(user);
        sqlSession.close();
        return borrowBookHistoryListByUser;
    }
}
